package com.scisoft.scan.ws;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scisoft.scan.data.QueryPageBean;
import com.scisoft.scan.data.check.CheckOrderBean;
import com.scisoft.scan.data.depot.DepotOrderStatis;
import com.scisoft.scan.data.depot.DepotQueryBean;
import com.scisoft.scan.data.depotoutchange.DOCOrderBean;
import com.scisoft.scan.data.depotoutchange.DOCOrderDetailBean;
import com.scisoft.scan.data.depotoutchange.DOCOrderStatisBean;
import com.scisoft.scan.data.depotoutchange.DOCPartInfoBean;
import com.scisoft.scan.data.stock.StockInfoBean;

import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.HeaderProperty;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.util.ArrayList;
import java.util.List;


public class WebServiceHelper_DepotOutChange {

    public static  String GetWS_Url(){
        return String.format("http://%s/dymps//ws/DepotOutChangeService?wsdl", SysParameter.WS_IPandPort);
    }

    public  static  String WS_Method = "receive";

    public  static  String WS_Namespace = "http://depotoutchange.scan.ws.dms.com/";


    //调拨出库订单统计
    public static DOCOrderStatisBean DEPOT_OUT_CHANGE_01() throws Exception {
//        DOCOrderStatisBean ret = new DOCOrderStatisBean();
//        ret.undoOrderCount = 1;
//        ret.doneOrderCount = 8;
//        return ret;
        DOCOrderStatisBean ret = new DOCOrderStatisBean();
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_01\",\"scSequence\":\"%s\",\"account\":\"%s\"}", SysParameter.ScSequence,SysParameter.user);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson rgson = new Gson();
            ret = rgson.fromJson(jsonArray.toString(),new TypeToken<DOCOrderStatisBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询调拨出库订单  orderStatus 1未审核 2已审核
    public static List<DOCOrderBean> DEPOT_OUT_CHANGE_02(String orderStatus) throws Exception {

//        List<DOCOrderBean> ret = new ArrayList<>();
//        DOCOrderBean orderBean = new DOCOrderBean();
//        orderBean.orderNo = "此处显示订单编号";
//        orderBean.optime = "10月11日";
//        orderBean.orderInfo = "共10项50件";
//        orderBean.amount = "234128.2元";
//        orderBean.operator = "张三";
//        orderBean.depotName = "J仓";
//        ret.add(orderBean);
//        return ret;
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_02\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,orderStatus);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<DOCOrderBean> ret = new ArrayList<>();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<DOCOrderBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询一个调拨出库订单的详细信息
    public static List<DOCOrderDetailBean> DEPOT_OUT_CHANGE_03(String orderId) throws Exception {

//        List<DOCOrderDetailBean> ret = new ArrayList<>();
//        DOCOrderDetailBean bean = new DOCOrderDetailBean();
//        bean.dtlId = "1";
//        bean.partNo = "P0001";
//        bean.partName = "汽车轮胎部件";
//        bean.supplierCode = "SQ203422323";
//        bean.supplierName = "山西大运汽车公司";
//        bean.partCount = "3";
//        bean.ifGyssm = "0";
//        bean.price = "258.3";
//        bean.serialNo = "AAAAAAAA2842334";
//        ret.add(bean);
//        return ret;
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_03\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        List<DOCOrderDetailBean> ret = new ArrayList<>();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret= gson.fromJson(jsonArray.toString(),new TypeToken<List<DOCOrderDetailBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //上传一个配件
    public static String DEPOT_OUT_CHANGE_04(String orderId, DOCOrderDetailBean docOrderDetailBean)throws Exception {

//        return "成功";
        Gson gson = new Gson();
        String parm =gson.toJson(docOrderDetailBean);
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_04\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //修改一个配件数量
    public static String DEPOT_OUT_CHANGE_05(String orderId,DOCOrderDetailBean docOrderDetailBean)throws Exception {

//        return "成功";
        Gson gson = new Gson();
        String parm =gson.toJson(docOrderDetailBean);
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_05\",\"account\":\"%s\",\"jsonData\":%s,\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user,parm, SysParameter.ScSequence,orderId);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {

        }
        else
        {
            throw new Exception(msg);
        }
        return msg;
    }

    //删除一个配件
    public static String DEPOT_OUT_CHANGE_06(String orderId,String dtlId)throws Exception {

//        return "成功";
        String bizId = orderId +","+dtlId;
        String arg0 = String.format("{\"flowNo\":\"DEPOT_OUT_CHANGE_06\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, bizId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //完成订单
    public static String DEPOT_OUT_CHANGE_07(String orderId)throws Exception {

//        return "成功";
        String arg0 = String.format("{\"flowNo\":\"DEPOT_OUT_CHANGE_07\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}", SysParameter.user, SysParameter.ScSequence, orderId);
        SoapObject soapObject = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg != null && msg.contains("成功")) {
        } else {
            throw new Exception(msg);
        }
        return msg;
    }

    //查询零售配件的分页信息 bizId:模糊搜索的条件
    public static QueryPageBean DEPOT_OUT_CHANGE_08(String bizId,String orderId) throws Exception {
//        QueryPageBean ret = new QueryPageBean();
//        ret.totalCount  = 100;
//        ret.pageSize = 10;
//        ret.pageCount = 5;
//        return ret;
        if(bizId==null ||  "".equals(bizId)){
            bizId="@@";
        }
        String biz = bizId + ","+ orderId;
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_08\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        QueryPageBean ret = new QueryPageBean();
        if (msg!=null&&msg.contains("成功"))
        {
            JSONObject jsonArray = data.getJSONObject("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<QueryPageBean>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    //查询零售配件列表 bizId:模糊搜索的条件 pagesize 每页的数量 pageindex 第几页,页码应该从1开始
    public static List<DOCPartInfoBean> DEPOT_OUT_CHANGE_09(String bizId,String orderId, int pagesize, int pageindex) throws Exception {
//        List<DOCPartInfoBean> ret = new ArrayList<>();
//        DOCPartInfoBean docPartInfoBean = new DOCPartInfoBean();
//        docPartInfoBean.partCode = "1232";
//        docPartInfoBean.partNo = "P23232";
//        docPartInfoBean.partName = "汽车方向盘";
//        docPartInfoBean.manufacturer = "SQ2323";
//        docPartInfoBean.manufacturerName = "大运";
//        docPartInfoBean.stockCount = "100";
//        docPartInfoBean.canUseCount = "40";
//        docPartInfoBean.depotName = "J仓";
//        docPartInfoBean.position = "A区223";
//        docPartInfoBean.ifGyssm = "0";
//        docPartInfoBean.price = 125.1;
//        ret.add(docPartInfoBean);
//        return ret;
        if(bizId==null ||  "".equals(bizId)){
            bizId="@@";
        }
        String biz = bizId + ","+ orderId;
        List<DOCPartInfoBean> ret = new ArrayList<>();
        String arg0 = String.format( "{\"flowNo\":\"DEPOT_OUT_CHANGE_09\",\"account\":\"%s\",\"scSequence\":\"%s\",\"bizId\":\"%s\",\"pageIndex\":\"%s\",\"pageSize\":\"%s\"}",SysParameter.user, SysParameter.ScSequence,biz,pageindex,pagesize);
        SoapObject soapObject  = CallWebservice(arg0);
        // 解析json
        String json = soapObject.getProperty(0).toString();
        JSONObject data = new JSONObject(json);
        String msg = data.optString("mESSAGE");
        if (msg!=null&&msg.contains("成功"))
        {
            JSONArray jsonArray = data.getJSONArray("jsonData");
            Gson gson = new Gson();
            ret = gson.fromJson(jsonArray.toString(),new TypeToken<List<DOCPartInfoBean>>(){}.getType());
        }
        else
        {
            throw new Exception(msg);
        }
        return ret;
    }

    public static  SoapObject CallWebservice(String arg0) throws Exception {
        //实例化SoapObject对象：命名空间+方法名称
        SoapObject request = new SoapObject(WS_Namespace, WS_Method);
        //设置WebService接口需要传入的参数
        request.addProperty("arg0", arg0);

        //创建SoapSerializationEnvelope 对象，同时指定soap版本号
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
        envelope.bodyOut = request;//由于是发送请求，所以是设置bodyOut
        envelope.dotNet = false;//由于是.net开发的webservice，所以这里要设置为true

        //调用接口
        ArrayList<HeaderProperty> headerPropertyArrayList = new ArrayList<HeaderProperty>();
        headerPropertyArrayList.add(new HeaderProperty("Connection", "close"));
        HttpTransportSE httpTransportSE = new HttpTransportSE(GetWS_Url());
        httpTransportSE.call(null, envelope,headerPropertyArrayList);//调用

        // 获取返回的数据
        SoapObject result = (SoapObject) envelope.bodyIn;
        return result;
    }
}
